BUUCTF Reverse WriteUp

easyre

I opened the file in IDA and checked it with PEiD, which reported that it was not a valid PE file.

In main, the program reads two integers and prints the flag if they are equal. That is the whole check:

1
2
3
4
5
6
7
8
9
10
11
12
13
int __cdecl main(int argc, const char **argv, const char **envp)
{
int b; // [rsp+28h] [rbp-8h]
int a; // [rsp+2Ch] [rbp-4h]

_main();
scanf("%d%d", &a, &b);
if ( a == b )
printf("flag{this_Is_a_EaSyRe}");
else
printf("sorry,you can't get flag");
return 0;
}

flag{this_Is_a_EaSyRe}

reverse1

IDA identifies this one as a 64-bit executable. I started with the error message because it should lead back to the check.

I used Shift+F12 to find the string wrong flag, then followed its references to main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
__int64 sub_1400118C0()
{
char *v0; // rdi
signed __int64 i; // rcx
size_t v2; // rax
size_t v3; // rax
char v5; // [rsp+0h] [rbp-20h]
int j; // [rsp+24h] [rbp+4h]
char Str1; // [rsp+48h] [rbp+28h]
unsigned __int64 v8; // [rsp+128h] [rbp+108h]

v0 = &v5;
for ( i = 82i64; i; --i )
{
*(_DWORD *)v0 = -858993460;
v0 += 4;
}
for ( j = 0; ; ++j )
{
v8 = j;
v2 = j_strlen(Str2);
if ( v8 > v2 )
break;
if ( Str2[j] == 111 )
Str2[j] = 48;
}
sub_1400111D1("input the flag:");
sub_14001128F("%20s", &Str1);
v3 = j_strlen(Str2);
if ( !strncmp(&Str1, Str2, v3) )
sub_1400111D1("this is the right flag!\n");
else
sub_1400111D1("wrong flag\n");
sub_14001113B(&v5, &unk_140019D00);
return 0i64;
}

Str2 contains the flag. A breakpoint could reveal it directly, but I decided to work backward through the logic.

The loop changes each o in Str2 to 0. Tracing the original value gives {hello_world}.

Submit flag{hell0_w0rld}.

reverse2

This time the file is an x86-64 ELF. In main, a fork initially looks suspicious, but there is also a character-by-character transformation to inspect:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int stat_loc; // [rsp+4h] [rbp-3Ch]
int i; // [rsp+8h] [rbp-38h]
__pid_t pid; // [rsp+Ch] [rbp-34h]
char s2; // [rsp+10h] [rbp-30h]
unsigned __int64 v8; // [rsp+28h] [rbp-18h]

v8 = __readfsqword(0x28u);
pid = fork();
if ( pid )
{
argv = (const char **)&stat_loc;
waitpid(pid, &stat_loc, 0);
}
else
{
for ( i = 0; i <= strlen(&flag); ++i )
{
if ( *(&flag + i) == 105 || *(&flag + i) == 114 )
*(&flag + i) = 49;
}
}
printf("input the flag:", argv);
__isoc99_scanf("%20s", &s2);
if ( !strcmp(&flag, &s2) )
result = puts("this is the right flag!");
else
result = puts("wrong flag!");
return result;
}

The fork creates another process, but the loop in the main thread is the quickest place to start:

1
2
3
4
5
for ( i = 0; i <= strlen(flag); ++i )
{
if ( flag[i] == 'i' || flag[i] == 'r' )
flag[i] = '1';
}

The loop replaces both i and r with 1.

Tracing shows the flag is {hacking_for_fun}, giving flag{hack1ng_fo1_fun} after the transformation.

That submission works. I expected the fork to hide a trap, but apparently not!

Happy New Year

PEiD reports UPX packing, but my usual unpacker cannot handle it. The latest official UPX does the job; PEiD then mentions yoda's Protector. I opened the result in IDA and went straight to main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
char v4; // [esp+12h] [ebp-3Ah]
__int16 v5; // [esp+20h] [ebp-2Ch]
__int16 v6; // [esp+22h] [ebp-2Ah]

__main();
strcpy(&v4, "HappyNewYear!");
v5 = 0;
memset(&v6, 0, 0x1Eu);
printf("please input the true flag:");
scanf("%s", &v5);
if ( !strncmp((const char *)&v5, &v4, strlen(&v4)) )
result = puts("this is true flag!");
else
result = puts("wrong!");
return result;
}

The flag appears directly: HappyNewYear!.

Software with a Hidden Meaning

IDA lists a lot of functions in this x86 executable, so I began with the strings and also ran the program to see what it does.

The program’s output is entertaining:

1
2
3
4
5
6
7
8
9
10
11
12
13
PS .\70125468-0786-4705-bd91-87037f8f3e16.exe
距离出现答案还有5秒,请耐心等待!
距离出现答案还有4秒,请耐心等待!
距离出现答案还有3秒,请耐心等待!
距离出现答案还有2秒,请耐心等待!
距离出现答案还有1秒,请耐心等待!
距离出现答案还有0秒,请耐心等待!



这里本来应该是答案的,但是粗心的程序员忘记把变量写进来了,你要不逆向试试看:(Y/N)
Y
OD吾爱破解或者IDA这些逆向软件都挺好的!

The strings in main include DBAPP{49d3c93df25caad81232130f3d2ebfad}. I tried submitting it, but no luck.

The hint supplies the missing detail: replace DBAPP with flag. Accepted!

guessgame

Running the program shows a menu. In IDA, option 1 calls game(), so that is where I looked next:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // [rsp+2Ch] [rbp-4h]

_main();
v4 = 0;
v3 = time(0i64);
srand(v3);
while ( 1 )
{
menu();
printf(QingXuanZe);
scanf("%d", &v4);
if ( v4 == 1 )
{
game();
}
else if ( v4 == 2 )
{
puts("想啥呢弟弟");
}
else if ( v4 )
{
puts(XuanZeCuoWu);
}
else
{
puts("退出游戏");
}
}
}

After giving the variables more useful names, I stepped into game:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int game()
{
int input; // [rsp+28h] [rbp-8h]
int randNum; // [rsp+2Ch] [rbp-4h]

input = 0;
randNum = rand() % 100 + 1;
while ( 1 )
{
while ( 1 )
{
printf(QingShuRuShuZi);
scanf("%d", &input);
if ( input <= randNum )
break;
puts(DaLe);
}
if ( input >= randNum )
break;
puts(XiaoLe);
}
return puts(aFlaga);
}

It is essentially a number-guessing game with no reward for a correct guess. In the strings window, the first string is the flag, BJD{S1mple_ReV3r5e_W1th_0D_0r_IDA}, which submits successfully.

Reversing it shows the string hidden in a menu variable, probably using inline assembly. It is not visible in the pseudocode but appears immediately in the assembly.

helloworld

The download is an Android APK, and I had never reversed one before. Time to find a tutorial.

The tutorials point to dex2jar for converting the APK’s DEX into a JAR. Opening that JAR in jar-decompiler brings the flag into view almost immediately.

Here is the source of MainActivity.class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.helloword;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(2130903064);
"flag{7631a988259a00816deda84afb29430a}".compareTo("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}

public boolean onCreateOptionsMenu(Menu paramMenu) {
getMenuInflater().inflate(2131492864, paramMenu);
return true;
}

public boolean onOptionsItemSelected(MenuItem paramMenuItem) {
return (paramMenuItem.getItemId() == 2131034172) ? true : super.onOptionsItemSelected(paramMenuItem);
}
}

There is the flag, right in the activity.

xor

This download turns out to be a macOS program, but IDA can still decompile main. The comparison loop is the part to watch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rsi
int result; // eax
signed int i; // [rsp+2Ch] [rbp-124h]
char flag[264]; // [rsp+40h] [rbp-110h]
__int64 v7; // [rsp+148h] [rbp-8h]

memset(flag, 0, 0x100uLL);
v3 = (char *)256;
printf("Input your flag:\n", 0LL);
get_line(flag, 256LL);
if ( strlen(flag) != 33 )
goto failed;
for ( i = 1; i < 33; ++i )
flag[i] ^= flag[i - 1];
v3 = global;
if ( !strncmp(flag, global, 0x21uLL) )
printf("Success", v3);
else
failed:
printf("Failed", v3);
result = __stack_chk_guard;
if ( __stack_chk_guard == v7 )
result = 0;
return result;
}

The loop XORs each flag character with the preceding one and compares it with a built-in byte. That gives me a way to recover the input sequentially.

I wrote a small script to undo the XORs:

1
2
3
4
5
6
7
8
9
10
data = [
0x66, 0x0A, 0x6B, 0x0C, 0x77, 0x26, 0x4F, 0x2E, 0x40, 0x11,
0x78, 0x0D, 0x5A, 0x3B, 0x55, 0x11, 0x70, 0x19, 0x46, 0x1F,
0x76, 0x22, 0x4D, 0x23, 0x44, 0x0E, 0x67, 0x06, 0x68, 0x0F,
0x47, 0x32, 0x4F, 0x00
]
for i in range(33, 0, -1):
data[i] ^= data[i-1]

print(''.join([chr(x) for x in data]))

The result is flag{QianQiuWanDai_YiTongJiangHu}.

reverse3

In IDA, main takes the input through a transformation before checking it against a stored value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
__int64 main_0()
{
int flagLength; // eax
const char *v1; // eax
size_t v2; // eax
int v3; // edx
__int64 v4; // ST08_8
signed int j; // [esp+DCh] [ebp-ACh]
signed int i; // [esp+E8h] [ebp-A0h]
signed int v8; // [esp+E8h] [ebp-A0h]
char Dest[108]; // [esp+F4h] [ebp-94h]
char flag; // [esp+160h] [ebp-28h]
char v11; // [esp+17Ch] [ebp-Ch]

for ( i = 0; i < 100; ++i )
{
if ( (unsigned int)i >= 0x64 )
j____report_rangecheckfailure();
Dest[i] = 0;
}
print("please enter the flag:");
scanf("%20s", &flag);
flagLength = j_strlen(&flag);
v1 = (const char *)sub_4110BE((int)&flag, flagLength, (int)&v11);
strncpy(Dest, v1, 40u);
v8 = j_strlen(Dest);
for ( j = 0; j < v8; ++j )
Dest[j] += j;
v2 = j_strlen(Dest);
if ( !strncmp(Dest, Str2, v2) )
print("rigth flag!\n");
else
print("wrong flag!\n");
HIDWORD(v4) = v3;
LODWORD(v4) = 0;
return v4;
}

Unlike the previous check, this one first calls a transformation function, then adds each character’s index before comparing the result with the target.

The transformation function is the next place to look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
void *__cdecl sub_411AB0(char *flag, unsigned int flagLength, int *a3)
{
int v4; // STE0_4
int v5; // STE0_4
int v6; // STE0_4
int v7; // [esp+D4h] [ebp-38h]
signed int i; // [esp+E0h] [ebp-2Ch]
unsigned int v9; // [esp+ECh] [ebp-20h]
int v10; // [esp+ECh] [ebp-20h]
signed int v11; // [esp+ECh] [ebp-20h]
void *Dst; // [esp+F8h] [ebp-14h]
char *v13; // [esp+104h] [ebp-8h]

if ( !flag || !flagLength )
return 0;
v9 = flagLength / 3;
if ( (flagLength / 3) % 3 )
++v9;
v10 = 4 * v9;
*a3 = v10;
Dst = malloc(v10 + 1);
if ( !Dst )
return 0;
j_memset(Dst, 0, v10 + 1);
v13 = flag;
v11 = flagLength;
v7 = 0;
while ( v11 > 0 )
{
byte_41A144[2] = 0;
byte_41A144[1] = 0;
byte_41A144[0] = 0;
for ( i = 0; i < 3 && v11 >= 1; ++i )
{
byte_41A144[i] = *v13;
--v11;
++v13;
}
if ( !i )
break;
switch ( i )
{
case 1:
*(Dst + v7) = aAbcdefghijklmn[byte_41A144[0] >> 2];
v4 = v7 + 1;
*(Dst + v4++) = aAbcdefghijklmn[((byte_41A144[1] & 0xF0) >> 4) | 16 * (byte_41A144[0] & 3)];
*(Dst + v4++) = aAbcdefghijklmn[64];
*(Dst + v4) = aAbcdefghijklmn[64];
v7 = v4 + 1;
break;
case 2:
*(Dst + v7) = aAbcdefghijklmn[byte_41A144[0] >> 2];
v5 = v7 + 1;
*(Dst + v5++) = aAbcdefghijklmn[((byte_41A144[1] & 0xF0) >> 4) | 16 * (byte_41A144[0] & 3)];
*(Dst + v5++) = aAbcdefghijklmn[((byte_41A144[2] & 0xC0) >> 6) | 4 * (byte_41A144[1] & 0xF)];
*(Dst + v5) = aAbcdefghijklmn[64];
v7 = v5 + 1;
break;
case 3:
*(Dst + v7) = aAbcdefghijklmn[byte_41A144[0] >> 2];
v6 = v7 + 1;
*(Dst + v6++) = aAbcdefghijklmn[((byte_41A144[1] & 0xF0) >> 4) | 16 * (byte_41A144[0] & 3)];
*(Dst + v6++) = aAbcdefghijklmn[((byte_41A144[2] & 0xC0) >> 6) | 4 * (byte_41A144[1] & 0xF)];
*(Dst + v6) = aAbcdefghijklmn[byte_41A144[2] & 0x3F];
v7 = v6 + 1;
break;
}
}
*(Dst + v7) = 0;
return Dst;
}

The alphabet and substitution table both look like Base64. I tried that hypothesis in a script, undoing the index adjustment first.

1
2
3
4
5
6
import base64
s1 = 'e3nifIH9b_C@n@dH'
s1 = list(s1)
for i in range(len(s1)):
s1[i] = chr(ord(s1[i]) - i)
print(base64.b64decode(''.join(s1)))

The flag is {i_l0ve_you}. It really was Base64.

A Different Flag

This x86 Windows program asks for directions numbered 1–4. The code updates coordinates, rejects walls and out-of-bounds moves, and succeeds on #: it is a maze.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // [esp+17h] [ebp-35h]
int x; // [esp+30h] [ebp-1Ch]
int y; // [esp+34h] [ebp-18h]
signed int input; // [esp+38h] [ebp-14h]
int i; // [esp+3Ch] [ebp-10h]
int map; // [esp+40h] [ebp-Ch]

__main();
x = 0;
y = 0;
qmemcpy(&v3, _data_start__, 0x19u);
while ( 1 )
{
puts("you can choose one action to execute");
puts("1 up");
puts("2 down");
puts("3 left");
printf("4 right\n:");
scanf("%d", &input);
if ( input == 2 )
{
++x;
}
else if ( input > 2 )
{
if ( input == 3 )
{
--y;
}
else
{
if ( input != 4 )
LABEL_13:
exit(1);
++y;
}
}
else
{
if ( input != 1 )
goto LABEL_13;
--x;
}
for ( i = 0; i <= 1; ++i )
{
if ( *(&x + i) < 0 || *(&x + i) > 4 )
exit(1);
}
if ( *(&map + 5 * x + y - 41) == '1' )
exit(1);
if ( *(&map + 5 * x + y - 41) == '#' )
{
puts("\nok, the order you enter is the flag!");
exit(0);
}
}
}

The assembly pushes a 25-character string. Since the coordinate checks use a five-by-five grid, this looks like the map:

*11110100001010000101111#, arranged five characters per row:

1
2
3
4
5
*1111
01000
01010
00010
1111#`

Tracing a route from the start to # gives the move sequence—and flag—222441144222.

SimpleRev

This is an x64 ELF. It will not run under WSL or Linux and immediately segfaults. Main is a menu; the important logic is in decry().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
unsigned __int64 Decry()
{
char v1; // [rsp+Fh] [rbp-51h]
int v2; // [rsp+10h] [rbp-50h]
int v3; // [rsp+14h] [rbp-4Ch]
int i; // [rsp+18h] [rbp-48h]
int v5; // [rsp+1Ch] [rbp-44h]
char src[8]; // [rsp+20h] [rbp-40h]
__int64 v7; // [rsp+28h] [rbp-38h]
int v8; // [rsp+30h] [rbp-30h]
__int64 v9; // [rsp+40h] [rbp-20h]
__int64 v10; // [rsp+48h] [rbp-18h]
int v11; // [rsp+50h] [rbp-10h]
unsigned __int64 v12; // [rsp+58h] [rbp-8h]

v12 = __readfsqword(0x28u);
*(_QWORD *)src = 'SLCDN';
v7 = 0LL;
v8 = 0;
v9 = 'wodah';
v10 = 0LL;
v11 = 0;
text = (char *)join(key3, &v9);
strcpy(key, key1);
strcat(key, src);
v2 = 0;
v3 = 0;
getchar();
v5 = strlen(key);
for ( i = 0; i < v5; ++i )
{
if ( key[v3 % v5] > '@' && key[v3 % v5] <= 90 )
key[i] = key[v3 % v5] + ' '; // 大写变小写
++v3;
}
printf("Please input your flag:", src);
while ( 1 )
{
v1 = getchar();
if ( v1 == 10 )
break;
if ( v1 == 32 )
{
++v2;
}
else
{
if ( v1 <= 96 || v1 > 122 )
{
if ( v1 > 64 && v1 <= 90 )
str2[v2] = (v1 - 39 - key[v3++ % v5] + 97) % 26 + 97;
}
else
{
str2[v2] = (v1 - 39 - key[v3++ % v5] + 97) % 26 + 97;
}
if ( !(v3 % v5) )
putchar(32);
++v2;
}
}
if ( !strcmp(text, str2) )
puts("Congratulation!\n");
else
puts("Try again!\n");
return __readfsqword(0x28u) ^ v12;
}

Inside decry(), the input is combined with a repeating key. The relevant values are:

text = killshadow key = adsfkndcls

For each position, the transformed input must equal the corresponding character of text. The expression to reverse is:

str2[idx] = (inputChar - '\'' - key[cnt++ % kenLength] + 'a') % 26 + 'a'

The modulo operation makes this awkward, but the following identity helps:
$$
(a + b) % c=(a % c + b % c)%c
$$
All key characters are lowercase, so their values cannot exceed 26; inputChar - '\' is roughly between 25 and 100.

I enumerated possible input characters for each position:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
text = list('killshadow')
text = [ord(x) for x in text]
key = list('adsfkndcls')
key = [ord(x) for x in key]
flag = [0] * len(text)
for i in range(len(text)):
flag[i] = text[i] - ord('a')
flag[i] += 26 * 2
flag[i] += key[i] - ord('a')
flag[i] += ord('\'')

#print(flag)
print(''.join([chr(x) for x in flag]))

KL^Q]UDFZi and efxkwo^t are strange flags, and neither submission is accepted.

An online writeup tests candidates directly because each character is encrypted independently:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def change(x:int, index:int):
char = (x - ord('\'') - (key[index] - ord('a'))) % 26 + ord('a')
return char

text = list('killshadow')
text = [ord(x) for x in text]
key = list('adsfkndcls')
key = [ord(x) for x in key]
flag = [0] * len(text)
#charset = list(range(ord('a'), ord('z')+1))
charset = list(range(ord('A'), ord('Z')+1))
for cnt in range(len(text)):
for i in charset:
res = change(i, cnt)
if res == text[cnt]:
flag[cnt] = i
print(''.join([chr(x) for x in flag]))

There are multiple solutions: uppercase KLDQCUDFZO and lowercase efxkwoxzti. In principle, mixing their characters should also satisfy the conditions.

Scratch to Win

IDA shows a Win32 windowed program. I can only see a scratch-card interface, so inspect the dialog logic in diagFunc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
BOOL __stdcall DialogFunc(HWND hDlg, UINT a2, WPARAM a3, LPARAM a4)
{
const char *v4; // esi
const char *v5; // edi
int key[11]; // [esp+8h] [ebp-20030h]
char input[8]; // [esp+34h] [ebp-20004h]
char chunk[3]; // [esp+10034h] [ebp-10004h]

if ( a2 == 272 )
return 1;
if ( a2 != 273 )
return 0;
if ( a3 == 1001 )
{
memset(input, 0, 0xFFFFu);
GetDlgItemTextA(hDlg, 1000, input, 0xFFFF);
if ( strlen(input) == 8 )
{
key[0] = 'Z';
key[1] = 'J';
key[2] = 'S';
key[3] = 'E';
key[4] = 'C';
key[5] = 'a';
key[6] = 'N';
key[7] = 'H';
key[8] = '3';
key[9] = 'n';
key[10] = 'g';
sub_4010F0(key, 0, 10);
memset(chunk, 0, 0xFFFFu);
chunk[0] = input[5];
chunk[2] = input[7];
chunk[1] = input[6];
v4 = sub_401000(chunk, strlen(chunk));
memset(chunk, 0, 0xFFFFu);
chunk[1] = input[3];
chunk[0] = input[2];
chunk[2] = input[4];
v5 = sub_401000(chunk, strlen(chunk));
if ( input[0] == key[0] + 34
&& input[1] == key[4]
&& 4 * input[2] - 141 == 3 * key[2]
&& input[3] / 4 == 2 * (key[7] / 9)
&& !strcmp(v4, "ak1w")
&& !strcmp(v5, "V1Ax") )
{
MessageBoxA(hDlg, "U g3t 1T!", "@_@", 0);
}
}
return 0;
}
if ( a3 != 1 && a3 != 2 )
return 0;
EndDialog(hDlg, a3);
return 1;
}

The two functions are visible here. The first, sub_4010F0, encrypts the key and appears to use recursion. Dynamic debugging in OllyDbg, with a few jumps changed, shows the key changing from ZJSECaNH3ng to 3CEHJNSZagn: a simple permutation.

Next inspect sub_401000. Its alphabet looks like Base64. The checks suggest values for input[0] and input[1], and checks on input[3] and input[4] can verify the guess. A modified Base64 alphabet also appears among the strings.

The result is UJWP1jMp.

Java Reverse Decryption

The archive contains a Java class. Its Encrypt method adds 64 to each character, XORs the result with 0x20, and compares the list with a hard-coded KEY:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.ArrayList;
import java.util.Scanner;

public class Reverse {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please input the flag );
String str = s.next();
System.out.println("Your input is );
System.out.println(str);
char[] stringArr = str.toCharArray();
Encrypt(stringArr);
}

public static void Encrypt(char[] arr) {
ArrayList<Integer> Resultlist = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
int result = arr[i] + 64 ^ 0x20;
Resultlist.add(Integer.valueOf(result));
}
int[] KEY = {
180, 136, 137, 147, 191, 137, 147, 191, 148, 136,
133, 191, 134, 140, 129, 135, 191, 65 };
ArrayList<Integer> KEYList = new ArrayList<>();
for (int j = 0; j < KEY.length; j++)
KEYList.add(Integer.valueOf(KEY[j]));
System.out.println("Result:");
if (Resultlist.equals(KEYList)) {
System.out.println("Congratulations);
} else {
System.err.println("Error);
}
}
}

Each position is independent, so I reversed those two operations in a short script:

1
2
3
4
5
6
table = [180, 136, 137, 147, 191, 137, 147, 191, 148, 136, 
133, 191, 134, 140, 129, 135, 191, 65]
for i in range(len(table)):
table[i] ^= 0x20
table[i] -= 64
print(''.join([chr(x) for x in table]))

flag{This_is_the_flag_!}

findit

For this APK, I used dex2jar again and opened the resulting JAR in Java Decompiler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.example.findit;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(2130903064);
Button button = (Button)findViewById(2131034173);
final EditText edit = (EditText)findViewById(2131034174);
final TextView text = (TextView)findViewById(2131034175);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View param1View) {
char[] arrayOfChar1 = new char[17];
char[] arrayOfChar2 = new char[38];
int i = 0;
while (true) {
String str;
if (i >= 17) {
if (String.valueOf(arrayOfChar1).equals(edit.getText().toString())) {
for (i = 0;; i++) {
if (i >= 38) {
str = String.valueOf(arrayOfChar2);
text.setText(str);
return;
}
if ((b[i] >= 'A' && b[i] <= 'Z') || (b[i] >= 'a' && b[i] <= 'z')) {
arrayOfChar2[i] = (char)(b[i] + 16);
if ((arrayOfChar2[i] > 'Z' && arrayOfChar2[i] < 'a') || arrayOfChar2[i] >= 'z')
arrayOfChar2[i] = (char)(arrayOfChar2[i] - 26);
} else {
arrayOfChar2[i] = b[i];
}
}
break;
}
} else {
if ((a[i] < 'I' && a[i] >= 'A') || (a[i] < 'i' && a[i] >= 'a')) {
str[i] = (char)(a[i] + 18);
} else if ((a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= 'a' && a[i] <= 'z')) {
str[i] = (char)(a[i] - 8);
} else {
str[i] = a[i];
}
i++;
continue;
}
text.setText(");
return;
}
}
});
}

public boolean onOptionsItemSelected(MenuItem paramMenuItem) {
return (paramMenuItem.getItemId() == 2131034176) ? true : super.onOptionsItemSelected(paramMenuItem);
}
}

The Java code transforms characters from strings a and b into two arrays, but the decompilation does not show me the arrays’ contents.

I unpacked the APK with apktools and found two MainActivity.smail files; one contained the array I needed. I copied it out:

1
2
3
4
table1 = [0x54, 0x68, 0x69, 0x73, 0x49, 0x73, 0x54, 0x68,
0x65, 0x46, 0x6c, 0x61, 0x67, 0x48, 0x6f, 0x6d, 0x65, ]
table2 = [0x70, 0x76, 0x6b, 0x71, 0x7b, 0x6d, 0x31, 0x36, 0x34, 0x36, 0x37, 0x35, 0x32, 0x36, 0x32, 0x30, 0x33, 0x33, 0x6c,
0x34, 0x6d, 0x34, 0x39, 0x6c, 0x6e, 0x70, 0x37, 0x70, 0x39, 0x6d, 0x6e, 0x6b, 0x32, 0x38, 0x6b, 0x37, 0x35, 0x7d, ]

With the missing data recovered, I copied the two arrays into IDEA and ran the code.

str is LzakAkLzwXdsyZgew, but submitting it fails. Inspecting table2 gives flag{c164675262033b4c49bdf7f9cda28a75}, which works.

8086

IDA recognizes a DOS file but does not identify any functions. The assembly is short enough to read directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
seg001:0000 sub_10030       proc near               ; CODE XREF: sub_10030↓j
seg001:0000 ; start+5↓p
seg001:0000 jmp short sub_10030
seg001:0000 sub_10030 endp
seg001:0000
seg001:0002 ; ---------------------------------------------------------------------------
seg001:0002 mov cx, 22h ; '"'
seg001:0005 lea bx, aUDuTZWjQGjzZWz ; "]U[du~|t@{z@wj.}.~q@gjz{z@wzqW~/b;"
seg001:0009
seg001:0009 loc_10039: ; CODE XREF: seg001:000F↓j
seg001:0009 mov di, cx
seg001:000B dec di
seg001:000C xor byte ptr [bx+di], 1Fh
seg001:000F loop loc_10039
seg001:0011 lea dx, aUDuTZWjQGjzZWz ; "]U[du~|t@{z@wj.}.~q@gjz{z@wzqW~/b;"
seg001:0015 mov ah, 9
seg001:0017 int 21h ; DOS - PRINT STRING
seg001:0017 ; DS:DX -> string terminated by "$"
seg001:0019 retn
seg001:001A assume ss:dseg, ds:nothing
seg001:001A
seg001:001A ; =============== S U B R O U T I N E =======================================
seg001:001A
seg001:001A ; Attributes: noreturn
seg001:001A
seg001:001A public start
seg001:001A start proc near
seg001:001A mov ax, seg dseg
seg001:001D mov ds, ax
seg001:001F assume ds:dseg
seg001:001F call sub_10030
seg001:001F start endp
seg001:001F
seg001:0022 ; ---------------------------------------------------------------------------
seg001:0022 mov ah, 4Ch
seg001:0024 int 21h ; DOS - 2+ - QUIT WITH EXIT CODE (EXIT)
seg001:0024 seg001 ends ; AL = exit code
seg001:0024
seg001:0024
seg001:0024 end start

The loop XORs each stored character with 0x1F. Applying the same operation again should recover the text.

I used a short script:

1
2
3
4
5
6
7
a = ']U[du~|t@{z@wj.}.~q@gjz{z@wzqW~/b;'
a = list(a)
a = [ord(x) for x in a]
flag = []
for i in a:
flag.append(i ^ 0x1f)
print(''.join([chr(x) for x in flag]))

The result is BJD{jack_de_hu1b1an_xuede_henHa0}.

check_1n

The program opens with an ASCII-art laptop. IDA lists many functions and main is not obvious, so I searched for the function that prints the laptop and followed its references.

There are too many strings for easy static analysis, so use OllyDbg.

OllyDbg does not reveal much either, but there is a strange string: 2i9Q8AtFJTfL3ahU2XGuemEqZJ2ensozjg1EjPJwCHy4RY1Nyvn1ZE1bZe.

There appears to be a decryption function, but the encryption routine is hard to read. Jumping from main in OllyDbg gets stuck at malloc, and there are anti-debugging checks along the way.

Finally, Shift+F12 reveals the startup password HelloWorld.

A flag file contains a fake flag. Base64 decoding gives Why don't you try the magic brick game.

That sounds like an invitation to try the brick-breaking game.

I lost the game—and the real flag appeared anyway!

flag{f5dfd0f5-0343-4642-8f28-9adbb74c4ede}